<?php
/**
 *
 * @author 
 *        
 *
 *        
 */
class Settle{
/*
*修改以及取消 造成的重新计算采购详情订单的金额以及采购单运费
*/
	public static function change_detail_money($p_detail_id,$data,$remark)
	{
		if($remark==90)
		{
			$transportation_expense = ($data['ware_quantity']/$data['quantity'])*$data['transportation_expense'];
			$data['quantity'] = $data['ware_quantity'];
			$arr = self::settle_detail_purchase($data);
			DB::update('purchaseorder_details')->set(
				array(
							 'money'                  => $arr['money'],
							 'transportation_expense' => $transportation_expense,	
							 'tax_money'							=> $arr['tax_money'],
							 'no_tax_money' 					=> $arr['no_tax_money'],								
			))
 			->where('id', '=', $p_detail_id)
      ->execute('newerp');
			self::change_purchase_expense($data['purchaseorder_id']);						
		}else
		{
			self::change_purchase_expense($data['purchaseorder_id']);
			/*$arr = array();
			$arr['id'] = $data['purchaseorder_id'];*/
            $arr = DB::select()->from('purchaseorders')->
                where('id','=',$data['purchaseorder_id'])->execute('newerp')->current();
			Settle::settle_purchase($arr);
		}
	}
/*
*重新计算采购订单运费
*/
	public static function change_purchase_expense($p_id)
	{
		$transportation_expense = DB::select(array(DB::expr('sum(transportation_expense)'),'transportation_expense'))
					->from('purchaseorder_details')
					->where('purchaseorder_id','=',$p_id)
					->where('status','<',100)->execute('newerp')->get('transportation_expense');
		$money = self::calculate_purchase_money($p_id);
		DB::update('purchaseorders')->set(array('transportation_expense' => $transportation_expense,
					'money' => $money
		))
		->where('id','=',$p_id)->execute('newerp');
	}
/*
*重新计算采购订单关于金钱
*/
	public static function settle_purchase($arr)
	{
			$money = self::calculate_purchase_money($arr['id']);
			self::settle_transportation_expense($arr['id'],$arr['transportation_expense']);
			$update = DB::update('purchaseorders')->set(
             array(
							 'purchaseid'  => trim(trim($arr['purchaseid'],'n')),
							 'money'       => $money,
               'pay_way'     => $arr['pay_way'],
               'transportation_expense'  => $arr['transportation_expense'],
               'remark'     => $arr['remark'], 
           ))
           ->where('id', '=', trim(trim($arr['id'],'n')))
           ->execute('newerp');
	}
/*
*采购详情单的总额
*/
	public static function calculate_purchase_money($arr)
	{
		$money = 0;
		$moneys = DB::select(array(DB::expr('sum(money)'),'money'))
			->from('purchaseorder_details')
			->where('purchaseorder_id','=',$arr)
			->where('status','<',100)
			->execute('newerp')->as_array();
		if(!empty($moneys))
		{
			foreach($moneys as $value)
			{
				$money +=$value['money'];
			}
		}
		return $money;
	}
/*
*完结造成的运费改变
*/
	public static function settle_transportation_expense($id,$transportation_expense)
	{
		$quantity = 0;
		$p_detail_info = DB::select('id','quantity','ware_quantity','status')
				->from('purchaseorder_details')
				->where('status','<',100)
				->where('purchaseorder_id','=',trim(trim($id,'n')))->execute('newerp')->as_array();//print_r($p_detail_info);
		foreach($p_detail_info as $p_in)
		{
			if($p_in['status'] == 90)
			{
				$quantity += $p_in['ware_quantity'];				
				continue;				
			}
			$quantity += $p_in['quantity'];								
		}
		if($quantity==0){
		$single_fee = 0;
		}else{
		$single_fee = $transportation_expense/$quantity;
		}
		foreach($p_detail_info as $p_in)
		{
			if($p_in['status'] == 90)
			{
				$quantity_actual = $p_in['ware_quantity'];						
			}else
			{
				$quantity_actual = $p_in['quantity'];	
			}
			$update = DB::update('purchaseorder_details')->set(
             array(
							 'transportation_expense'        => $single_fee*$quantity_actual,
           ))
           ->where('id', '=', $p_in['id'])
           ->execute('newerp');
		}
	}
/*
*计税方式造成涉及金额的重新计算
*/
	public static function settle_detail_purchase($arr)
	{
		$money = 0;
		if(!empty($arr))
		{
			switch($arr['tax_way'])
			{
				case 0:
					$money = $arr['single_price']*$arr['quantity'];
					$tax_money = 0;
					$no_tax_money = $arr['single_price']*$arr['quantity'];
					break;
				case 1:
					
					$money = ($arr['single_price']*$arr['quantity'])*(1+$arr['tax_rate']/100);
					$tax_money = ($arr['single_price']*$arr['quantity'])*$arr['tax_rate']/100;
					$no_tax_money = $arr['single_price']*$arr['quantity'];						
					break;
				case -1:
					$money = $arr['single_price']*$arr['quantity'];
					$tax_money = ($arr['single_price']*$arr['quantity'])*(($arr['tax_rate']/100)/(1+$arr['tax_rate']/100));
					$no_tax_money = $arr['single_price']*$arr['quantity']*(1-($arr['tax_rate']/100)/(1+$arr['tax_rate']/100));						
					break;
				default:
					;					
			}
			$moneys = array('money'=>$money,'tax_money'=>$tax_money,'no_tax_money'=>$no_tax_money);	
			
		}
		return $moneys;		
	}
}

